﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace B.S.BasicData.Api.Write.Filter
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    public class GlobalExceptionFilter : IExceptionFilter
    {
        private readonly ILogger<GlobalExceptionFilter> logger;

        public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
        {
            this.logger = logger;
        }
        public void OnException(ExceptionContext context)
        {
            //全局发生了异常,都在这个方法里处理
            logger.LogError("捕捉到全局异常,异常内容为:"+context.Exception.Message);
            context.ExceptionHandled = true;//异常已经被过滤器处理过
        }
    }
}
